#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

#define PLUGIN_NAME                   "l4d2_tank_lizi_penqi"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "坦克粒子特效喷气"   //介绍
#define PLUGIN_VERSION                "1.0"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站

public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}

#define CVAR_FLAGS			FCVAR_NOTIFY


Handle hCvar_Trigger_timer;
float Trigger_timer;

int  g_TankFanWeiKill;
ConVar hCvar_TankFanWeiKill;

public void OnPluginStart()
{
	
	RegAdminCmd("sm_tankpenqi", Command_SmokerSwitch, ADMFLAG_KICK, "坦克粒子特效喷气指令开关");
	hCvar_TankFanWeiKill = CreateConVar("l4d2_tank_dlizi_penqi", "1", "是否启用坦克粒子特效喷气插件?", CVAR_FLAGS, true, 0.0, true, 1.0);
	HookConVarChange(hCvar_TankFanWeiKill, eConvarChanged);	


	HookEvent("tank_spawn", Event_Tank_Spawn, EventHookMode_Pre);
	
	hCvar_Trigger_timer = CreateConVar("l4d2_hCvar_tank_timer_penqi", "1.0", "坦克每多少秒发动特效?", CVAR_FLAGS, true, 0.1);
	HookConVarChange(hCvar_Trigger_timer, eConvarChanged);
	

	CreateConVar("l4d2_tank_lizi_penqi_version",	PLUGIN_VERSION,	"插件版本.", FCVAR_NOTIFY);
	//AutoExecConfig(true, "l4d2_tank_lizi_penqi");
}

public void OnMapStart()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	Trigger_timer = GetConVarFloat(hCvar_Trigger_timer);
	
	g_TankFanWeiKill = hCvar_TankFanWeiKill.IntValue;	
}

public Action Command_SmokerSwitch(int client, int args)
{
	if (g_TankFanWeiKill)
	{
		hCvar_TankFanWeiKill.IntValue = 0;
		PrintToChat(client, "\x04[提示]\x03Tank\x05粒子特效喷气插件已\x03关闭\x05.");
	}
	else
	{
		hCvar_TankFanWeiKill.IntValue = 1;
		PrintToChat(client, "\x04[提示]\x03Tank\x05粒子特效喷气插件已\x03开启\x05.");
	}
	return Plugin_Handled;
}

void Event_Tank_Spawn(Event event, const char[] name, bool dontBroadcast)
{
	int userid = GetEventInt(event, "userid");
	int client = GetClientOfUserId(userid);
	if (IsInfected(client)) 
	{
		CreateTimer(Trigger_timer, nexthealing_inf, userid, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);	
	}
}

public Action nexthealing_inf(Handle timer, any client)
{
	client = GetClientOfUserId(client);
	if(IsTank(client) && IsPlayerAlive(client) && g_TankFanWeiKill == 1)
	{
		//PrintToChatAll("\x04[提示]\x03坦克\x05非常愤怒，发动了\x03秒杀\x05！");

			
			float pos[3];
			char sName[64];
			char sTargetName[64];
			int Particle = CreateEntityByName("info_particle_system", -1);
			GetClientAbsOrigin(client, pos);
			TeleportEntity(Particle, pos, NULL_VECTOR, NULL_VECTOR);
			Format(sName, 64, "%d", client + 25);
			DispatchKeyValue(client, "targetname", sName);
			GetEntPropString(client, Prop_Data, "m_iName", sName, sizeof(sName));
			Format(sTargetName, 64, "%d", client + 1000);
			DispatchKeyValue(Particle, "targetname", sTargetName);
			DispatchKeyValue(Particle, "parentname", sName);
			DispatchKeyValue(Particle, "effect_name", "impact_steam_short");
			DispatchSpawn(Particle);
			SetVariantString(sName);
			AcceptEntityInput(Particle, "SetParent", Particle, Particle, 0);
			ActivateEntity(Particle);
			AcceptEntityInput(Particle, "start", -1, -1, 0);
			
			
		
	}
	return Plugin_Continue;
}

bool IsInfected(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 3;
}

bool IsTank(int client)  
{
	if (!IsInfected(client))
		return false;

	if (GetEntProp(client, Prop_Send, "m_zombieClass") != 8)
		return false;

	return true;
}





